package com.npjscg.medicalpro.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.controller.dto.ChartDTO;
import com.npjscg.medicalpro.controller.dto.HospitalDTO;
import com.npjscg.medicalpro.controller.dto.XYVO;
import com.npjscg.medicalpro.entity.Hospital;
import com.npjscg.medicalpro.exception.ServiceException;
import com.npjscg.medicalpro.mapper.HospitalMapper;
import com.npjscg.medicalpro.service.IDepService;
import com.npjscg.medicalpro.service.IHospitalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raote
 * @since 2022-07-04
 */
@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital> implements IHospitalService {
    //注入redis
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private HospitalMapper hospitalMapper;

    @Autowired
    private IDepService depService;

    public static final String key = "hospitalInfo";

    @Override
    public void addHospital(Hospital hospital) {
        Hospital byId = getById(hospital.getHcode());
        if (byId != null)
            throw new ServiceException(Constants.CODE_600, "已有该医疗信息请勿重复添加");
        else{
            flushRedis(key);
            save(hospital);
        }

    }

    @Override
    public List<Hospital> findAll(String hospital) {
        //有redis环境下启用
        ValueOperations valueOperations = stringRedisTemplate.opsForValue();
        String hospitalInfo = (String) valueOperations.get(key);
        if (hospitalInfo == null){
            QueryWrapper<Hospital> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("hospital", hospital);
            List<Hospital> hospitals = hospitalMapper.selectList(queryWrapper);
            valueOperations.set(key, JSONUtil.toJsonStr(hospitals));
            return hospitals;
        }
        else {
            return JSONUtil.toList(hospitalInfo, Hospital.class);
        }

        //无redis环境下启用
//        QueryWrapper<Hospital> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("hospital", hospital);
//        List<Hospital> hospitals = hospitalMapper.selectList(queryWrapper);
//        return hospitals;

    }

    @Override
    public XYVO getChart(String hcode) {
        Hospital byId = getById(hcode);
        if (byId == null)
            throw new ServiceException(Constants.CODE_400, "数据库中没有该医院数据");
        List<ChartDTO> chart = hospitalMapper.getChart(hcode);
        XYVO xyvo = new XYVO();
        List<String> x = new ArrayList<>();
        List<Integer> y = new ArrayList<>();
        for (ChartDTO chartDTO : chart) {
            x.add(chartDTO.getCheckdate().toString());
            y.add(chartDTO.getDoctornum());
        }
        xyvo.setXseries(x);
        xyvo.setYseries(y);
        System.out.println(xyvo);
        return xyvo;
    }

    @Override
    public void deleteHospital(String hcode) {
        int i = hospitalMapper.deleteById(hcode);
        if (i == 0)
            throw new ServiceException(Constants.CODE_600,"数据库中没有该医院信息！");
        else{
            flushRedis(key);
            return;
        }

    }

    @Override
    public void editHospital(Hospital hospital) {
        Hospital byId = getById(hospital.getHcode());
        if (byId == null)
            throw new ServiceException("-1", "数据库中没有该医院信息！");
        else {
            flushRedis(key);
            UpdateWrapper<Hospital> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("hcode", hospital.getHcode());
            updateWrapper.set("hospital",hospital.getHospital());
            updateWrapper.set("hrank", hospital.getHrank());
            update(updateWrapper);
        }

    }

    @Override
    public IPage hospitalPage(Page<Hospital> page, String hospital) {
        QueryWrapper<Hospital> queryWrapper = new QueryWrapper<>();
        if (hospital == null)
            hospital = "";
        queryWrapper.like("hospital", hospital );
        return hospitalMapper.selectPage(page, queryWrapper);
    }

    @Override
    public HospitalDTO checkOne(String hcode) {
        HospitalDTO hospitalDTO = hospitalMapper.checkOne(hcode);
        if (hospitalDTO == null)
            throw new ServiceException("-1", "该医院还未有过上报记录");
        return hospitalDTO;
    }

    @Override
    public IPage hospitalPageByDep(Page<Hospital> page, Integer did) {
        if (depService.getById(did) == null)
            throw new ServiceException("-1", "数据库中没有此数据！");
        else {
            QueryWrapper<Hospital> queryWrapper = new QueryWrapper<>();
            queryWrapper.groupBy("hcode");
            return hospitalMapper.hospitalPageByDep(page, queryWrapper, did);
        }

    }


    private void flushRedis(String keys) {
            stringRedisTemplate.delete(keys);
    }
}
